<?
	function GetLanguage($def)
	{
 		if(!isset($_SESSION['SESSION_CUR_LANG']))
    { 
      return $def;
    }else
 		{
 		   $lg = trim($_SESSION['SESSION_CUR_LANG']);
 		   $lg = strlen($lg)>0 ? $lg : $def;
 		   return $lg;
    }
	}

	function GetLanguageID($lg)
	{
		OB_START();
		$language = new LangBO();
		$language->short_name = $lg;
		$language->Load(0,0);
		$language->Next();  
		OB_END_CLEAN();
		return $language->id; 
	}

	function GetLanguageShortName($id)
	{
		OB_START();
		$language = new LangBO();
		$language->id = $id;
		$language->Load(0,0);
		$language->Next();  
		OB_END_CLEAN();
		return $language->short_name; 
	}
  
  $lg    = GETPOST("lg");
  $lg_id = -1;

  if(strlen(trim($lg))==0)
  {
    $lg    = GetLanguage(DEFAULT_LANGUAGE);
    $lg_id = GetLanguageID($lg);
  }else
  {
    if(is_numeric($lg))
    {
       $lg_id = $lg; 
       $lg = GetLanguageShortName($lg_id);   
    }else
    {
       $lg_id = GetLanguageID($lg);
    }
  }
        // rem just for lazy Doug :)
	// $_SESSION['SESSION_CUR_LANG'] = $lg;
        
	define('CURRENT_LANGUAGE',$lg);
	define('CURRENT_LANGUAGE_ID',$lg_id);
	$lang = array();  	
 
	require(LANGUAGES_FOLDER.'main.php'); 
	if(!file_exists(LANGUAGES_FOLDER.CURRENT_LANGUAGE.'/common.php')){
            $_SESSION['SESSION_CUR_LANG'] = DEFAULT_LANGUAGE;
            header('location: index.php');
            exit(0);
	}
 	require(LANGUAGES_FOLDER.CURRENT_LANGUAGE.'/common.php'); 
	
 	if(isset($lang_pages))
	{
            foreach($lang_pages as $page)
            {
                if(file_exists(LANGUAGES_FOLDER.$lg.'/'.$page.'.php'))
                {
                    require(LANGUAGES_FOLDER.$lg.'/'.$page.'.php');
                }
            }
	}
  //return prepared phrase
	function pf($f,$p="")
	{
  		return vsprintf($f,$p);
	}	
?>
